﻿import mx.utils.Delegate;

class com.unifiedfield.cConfigFile {
	public  var timeOut:Number;
	private var configXML:XML;
	private var m_sConfigPath:String;
	private var m_sJumpToLabel:String = null;
	var m_xmlMainNode:XMLNode;
	
	public function cConfigFile( sPath:String ,sJump:String ) 
		{
		m_sConfigPath = sPath;
		m_sJumpToLabel = sJump;
		loadConfig( m_sConfigPath );
		}

	public function loadConfig(sPath:String):Void 
		{
		configXML = new XML();
		configXML.ignoreWhite = true;
		configXML.onLoad = Delegate.create(this, onloadConfig);
		configXML.load(sPath);
		}
	
	function onloadConfig(success:Boolean):Void 
		{
		if (success) 
			{
			m_xmlMainNode = configXML.firstChild.firstChild.firstChild;
			trace("configXML: "+m_xmlMainNode);
			}
		else 
			{
			m_xmlMainNode = null;
			trace("ERROR - configXML DID NOT LOAD");
			}
		if( m_sJumpToLabel != null )
			{
			gotoAndStop(m_sJumpToLabel);
			}
		} // End onloadConfig()

	function GetValue( sTargetNodeName:String, iDefault:Number ):Number
		{
		var mainNode:XMLNode = configXML.firstChild.firstChild;

		while (mainNode != null) 
			{
			if( sTargetNodeName == mainNode.nodeName )
			  {
				return( Number(mainNode.firstChild.nodeValue) );
				}
			mainNode = mainNode.nextSibling;
			}
		return( iDefault );
		} // End GetValue
		
	function GetBool( sTargetNodeName:String, bDefault:Boolean ):Boolean
		{
		var sValue:String;
		var mainNode:XMLNode = configXML.firstChild.firstChild;

		while (mainNode != null) 
			{
			if( sTargetNodeName == mainNode.nodeName )
			  {
				if( mainNode.firstChild.nodeValue != null )
				  {
					sValue = 	mainNode.firstChild.nodeValue;
					if( sValue == "true" ||  sValue == "TRUE" ||  sValue == "True" || sValue == "1" )
						{
						return( true );
						}
					if( sValue == "false" ||  sValue == "FALSE" ||  sValue == "False" || sValue == "0" )
						{
						return( false );
						}
					}
				}
			mainNode = mainNode.nextSibling;
			}
		return( bDefault );
		} // End GetBool
		
	function GetString( sTargetNodeName:String, sDefault:String ):String
		{
		var mainNode:XMLNode = configXML.firstChild.firstChild;

		while( mainNode != null ) 
			{
			if( sTargetNodeName == mainNode.nodeName )
			  {
				if( mainNode.firstChild.nodeValue != null )
				  {
					return( mainNode.firstChild.nodeValue );
					}
				}
			mainNode = mainNode.nextSibling;
			}
		return( sDefault );
		} // End GetString
		
	function GetNumber( sTargetNodeName:String, iDefault:Number ):Number
		{
		var mainNode:XMLNode = configXML.firstChild.firstChild;

		while( mainNode != null ) 
			{
			if( sTargetNodeName == mainNode.nodeName )
			  {
				if( mainNode.firstChild.nodeValue != null )
				  {
					return( Number(mainNode.firstChild.nodeValue) );
					}
				}
			mainNode = mainNode.nextSibling;
			}
		return( iDefault );
		} // End GetNumber
		
	function GetFullConfig():Void 
		{
//		var mainNode:XMLNode = configXML.firstChild.firstChild.firstChild;
		var mainNode:XMLNode = configXML.firstChild.firstChild;
		//trace("mainNode " + mainNode);
		while(mainNode != null) 
			{
			trace("nodeName = " + mainNode.nodeName);
			trace("node Value = " + mainNode.firstChild.nodeValue);
			switch (mainNode.nodeName) 
				{
				case "timeOut" :
					timeOut = Number(mainNode.firstChild.nodeValue);
					trace("timeOut = " + timeOut);
					break;
				default :
					break;
				}
			mainNode = mainNode.nextSibling;
			}
		//trace("channelURL = " + channelURL);
		} // End GetFullConfig
	} // End class com.unifiedfield.cConfigFile()
